package com.yutel.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ReflexUtil {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("2012-12-12");
		list.add("2010-12-12");
		list.add("2011-11-12");
		list.add("2012-12-10");
		list.add("2012-01-12");
		ReflexUtil.print(list);
		ReflexUtil.sort(list);
		ReflexUtil.print(list);
	}

	public static void sort(ArrayList<String> l) {
		Collections.sort(l, new Comparator<Object>() {
			@Override
			public int compare(Object o1,Object o2) {
				String ct1=(String)o1;
				String ct2=(String)o2;
				return ct1.compareTo(ct2);
			}
		});
	}

	public static void print(ArrayList<String> l) {
		for (String item : l) {
			System.out.println(item);
		}
	}

	/**
	 * 按照指定属性，对List集合进行排序。
	 * 
	 * @param list
	 *            bean集合
	 * @param sortBy
	 *            要进行排序的bean中的属性
	 * @param sort
	 *            升序或降序
	 */
	
	/*
	public static <T> void sort(List<T> list, final String sortBy,
			final String sort) {
		if (list == null || sortBy == null || sortBy.equals("")
				|| list.isEmpty())
			return;
		Collections.sort(list, new Comparator<T>() {
			@SuppressWarnings("unchecked")
			public int compare(T t1, T t2) {
				Object o1 = null;
				Object o2 = null;
				try {
					o1 = ReflexUtil.getObjFieldValue(t1, sortBy);
					o2 = ReflexUtil.getObjFieldValue(t2, sortBy);
				} catch (Exception e) {
					e.printStackTrace();
				}
				int result = 0;
				if (o1 == null) {
					result = -1;
				} else if (o2 == null) {
					result = 1;
				}
				// 字符串按照拼音排序
				else if (o1 instanceof String) {
					result = Collator.getInstance(Locale.CHINA).compare(o1, o2);
				} else {
					result = ((Comparable) o1).compareTo(o2);
				}

				if (DESC.equalsIgnoreCase(sort)) {
					result = 0 - result;
				}
				return result;

			}
		});

	}
	*/
}
